package com.qumitech.visualnovel.service.novel;

import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
import java.util.*;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import javax.servlet.http.HttpServletRequest;
import com.qumitech.visualnovel.model.common.BaseResponse;
import com.qumitech.visualnovel.component.config.Config;
import com.qumitech.visualnovel.component.thirdapi.aliyun.AliYunOSSUtil;

import com.qumitech.visualnovel.model.dao.editor.EngineUpdateDAO;

import com.qumitech.visualnovel.datasource.editor.EngineUpdateDAOMapper;

@Slf4j
@Service
public class EngineServiceImpl implements EngineService {
    static final String ENGINE_UPDATE_PATH = Config.getProperty("app.engineUpdatePath");

    @Autowired
    EngineUpdateDAOMapper engineUpdateDAOMapper;

    @Override
    public BaseResponse getNewVersion() {
        JSONObject result = new JSONObject();
        EngineUpdateDAO engineUpdateDAO = engineUpdateDAOMapper.getMaxVersion();
        if (engineUpdateDAO != null) {
            result.put("mainVersion", engineUpdateDAO.getMainVersion());
            result.put("subVersion", engineUpdateDAO.getSubVersion());
        }
        return new BaseResponse(result);
    }

    @Override
    public BaseResponse upload(HttpServletRequest request) {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest)request;
        String strMainVersion = multipartRequest.getParameter("mainVersion");
        String strSubVersion = multipartRequest.getParameter("subVersion");
        MultipartFile file = multipartRequest.getFile("file");

        Integer mainVersion = Integer.valueOf(strMainVersion);
        Integer subVersion = Integer.valueOf(strSubVersion);
        if (mainVersion==null || subVersion==null || file==null || subVersion>99) {
            return new BaseResponse(-1, "参数错误");
        }

        EngineUpdateDAO engineUpdateDAO = engineUpdateDAOMapper.getMaxVersion();
        if (engineUpdateDAO != null) {
            Integer maxMainVersion = engineUpdateDAO.getMainVersion();
            Integer maxSubVersion = engineUpdateDAO.getSubVersion();
            if (maxMainVersion!=null && maxSubVersion!=null) {
                if (mainVersion*100+subVersion <= maxMainVersion*100+maxSubVersion) {
                    return new BaseResponse(-1, "新版本号必须大于老版本");
                }
            }
        }

        String originalName = file.getOriginalFilename();
        String ossPath = ENGINE_UPDATE_PATH + "/" + originalName;
        AliYunOSSUtil.uploadLocateFile(file, ossPath);

        if (engineUpdateDAO == null) engineUpdateDAO = new EngineUpdateDAO();
        engineUpdateDAO.setId(null);
        engineUpdateDAO.setMainVersion(mainVersion);
        engineUpdateDAO.setSubVersion(subVersion);
        engineUpdateDAO.setPath(ossPath);
        engineUpdateDAOMapper.insertSelective(engineUpdateDAO);

        log.info("update engine: mainVersion={}, subVersion={}, name={}, ossPath={}", mainVersion, subVersion, originalName, ossPath);
        return new BaseResponse();
    }
}
